class QuestionPaser():
    '''构建实体节点'''

    def build_entitydict(self, args):
        entity_dict = {}
        for arg, types in args.items():
            for type in types:
                if type not in entity_dict:
                    entity_dict[type] = [arg]
                else:
                    entity_dict[type].append(arg)

        return entity_dict

    '''解析主函数'''
    def parser_main(self, res_classify):
        args = res_classify['args']
        entity_dict = self.build_entitydict(args)
        question_types = res_classify['question_types']
        sqls = []
        for question_type in question_types:
            sql_ = {}
            sql_['question_type'] = question_type
            sql = []

            if question_type == 'stockid_conceptget':
                sql = self.sql_transfer(question_type, entity_dict.get('stockid'))

            elif question_type == 'stockname_conceptget':
                sql = self.sql_transfer(question_type, entity_dict.get('stockname'))

            elif question_type == 'concept_stockget':
                sql = self.sql_transfer(question_type, entity_dict.get('concept'))

            elif question_type == 'company_industryget':
                sql = self.sql_transfer(question_type, entity_dict.get('company'))

            elif question_type == 'stockid_industryget':
                sql = self.sql_transfer(question_type, entity_dict.get('stockid'))

            elif question_type == 'stockname_industryget':
                sql = self.sql_transfer(question_type, entity_dict.get('stockname'))

            elif question_type == 'industry_stockget':
                sql = self.sql_transfer(question_type, entity_dict.get('industry'))

            elif question_type == 'stockid_indextypeget':
                sql = self.sql_transfer(question_type, entity_dict.get('stockid'))

            elif question_type == 'stockname_indextypeget':
                sql = self.sql_transfer(question_type, entity_dict.get('stockname'))

            elif question_type == 'indextype_stockget':
                sql = self.sql_transfer(question_type, entity_dict.get('indextype'))

            elif question_type == 'stockid_cominfoget':
                sql = self.sql_transfer(question_type, entity_dict.get('stockid'))

            elif question_type == 'stockname_cominfoget':
                sql = self.sql_transfer(question_type, entity_dict.get('stockname'))

            elif question_type == 'company_cominfoget':
                sql = self.sql_transfer(question_type, entity_dict.get('company'))

            elif question_type == 'stockid_stockholderget':
                sql = self.sql_transfer(question_type, entity_dict.get('stockid'))

            elif question_type == 'stockname_stockholderget':
                sql = self.sql_transfer(question_type, entity_dict.get('stockname'))

            elif question_type == 'company_stockholderget':
                sql = self.sql_transfer(question_type, entity_dict.get('company'))

            elif question_type == 'stockid_chairmanget':
                sql = self.sql_transfer(question_type, entity_dict.get('stockid'))

            elif question_type == 'stockname_chairmanget':
                sql = self.sql_transfer(question_type, entity_dict.get('stockname'))

            elif question_type == 'company_chairmanget':
                sql = self.sql_transfer(question_type, entity_dict.get('company'))

            elif question_type == 'stockid_subcompanyget':
                sql = self.sql_transfer(question_type, entity_dict.get('stockid'))

            elif question_type == 'stockname_subcompanyget':
                sql = self.sql_transfer(question_type, entity_dict.get('stockname'))

            elif question_type == 'company_subcompanyget':
                sql = self.sql_transfer(question_type, entity_dict.get('company'))

            if sql:
                sql_['sql'] = sql

                sqls.append(sql_)

        return sqls

    '''针对不同的问题，分开进行处理'''
    def sql_transfer(self, question_type, entities):
        if not entities:
            print('in not entities\n')
            return []

        # 查询语句
        sql = []

        #按股票代码查询所属概念
        if question_type == 'stockid_conceptget':
            sql = ["MATCH (m:公司)-[r:涉及]->(n:概念) where m.股票代码 = '{0}' return m.股票代码, m.股票名称,n.名称".format(i) for i in entities]
            # print(sql)

        #按股票名称查询所属概念
        elif question_type == 'stockname_conceptget':
            sql = ["MATCH (m:公司)-[r:涉及]->(n:概念) where m.股票名称 = '{0}' return m.股票代码, m.股票名称,n.名称".format(i) for i in entities]

        elif question_type == 'concept_stockget':
            length = len(entities)
            part1_list = []
            part2_list = []
            part3_list = []
            for i in range(length):
                part1_list.append("(m:公司)-[r#i#:涉及]->(n#i#:概念)".replace('#i#', str(i)))
                part2_list.append("n#i#.名称 = '{0}'".format(entities[i]).replace('#i#', str(i)))
                part3_list.append("n#i#.名称".replace('#i#', str(i)))
            part1 = ",".join(part1_list)
            part2 = " and ".join(part2_list)
            part3 = ','.join(part3_list)
            sql = ["MATCH #part1# where #part2# return m.股票代码, m.股票名称, #part3#".replace('#part1#', part1).replace('#part2#', part2).replace('#part3#', part3)]

        # 按公司名称查询董事长
        elif question_type == 'company_chairmanget':
            sql = [
                 "MATCH (m:高管)-[r:任职]->(n:公司) where r.职位='董事长' and n.名称 = '{0}' return m.名称,m.年龄,m.性别,m.学历,m.薪酬,n.名称,n.股票代码".format(i) for i in entities]

        #按股票代码查询董事长
        elif question_type == 'stockid_chairmanget':
            sql = ["MATCH (m:高管)-[r:任职]->(n:公司) where r.职位='董事长' and n.股票代码 = '{0}' return m.名称,m.年龄,m.性别,m.学历,m.薪酬,n.名称,n.股票代码".format(i) for i in entities]
            print(sql)

        #按股票名称查询董事长
        elif question_type == 'stockname_chairmanget':
            sql = ["MATCH (m:高管)-[r:任职]->(n:公司) where r.职位='董事长' and n.股票名称 = '{0}' return m.名称,m.年龄,m.性别,m.学历,m.薪酬,n.名称,n.股票代码".format(i) for i in entities]

        #根据高管查询公司
        elif question_type == 'chairman_companyget':
            sql = ["MATCH (m:{0})-[r:IsControlledBy]->(n:Controller) where n.name = '{1}' return m.stock_id, m.stock_name, n.name".format(self.pDate, i) for i in entities]

        #根据概念查龙头股
        elif question_type == 'conceptleading_stockget':
            length = len(entities)
            part1_list = []
            part2_list = []
            part3_list = []
            for i in range(length):
                part1_list.append("(m:{0})-[r#i#:ConceptLeadingInvolved]->(n#i#:ConceptLeading)".format(self.pDate).replace('#i#', str(i)))
                part2_list.append("n#i#.name = '{0}'".format(entities[i]).replace('#i#', str(i)))
                part3_list.append("n#i#.name".replace('#i#', str(i)))
            part1 = ",".join(part1_list)
            part2 = " and ".join(part2_list)
            part3 = ','.join(part3_list)
            sql = ["MATCH #part1# where #part2# return m.stock_id, m.stock_name, #part3#".replace('#part1#', part1).replace('#part2#', part2).replace('#part3#', part3)]

        # 按公司名称查询所属行业
        elif question_type == 'company_industryget':
            sql = [
                "MATCH (m:公司)-[r:属于]->(n:行业) where m.名称 = '{0}' return m.股票代码, m.股票名称,m.名称, r.级别, n.名称".format(
                    i) for i in entities]

        #按股票代码查询所属行业
        elif question_type == 'stockid_industryget':
            sql = ["MATCH (m:公司)-[r:属于]->(n:行业) where m.股票代码 = '{0}' return m.股票代码, m.股票名称, r.级别, n.名称".format(i) for i in entities]

        #按股票名称查询所属行业
        elif question_type == 'stockname_industryget':
            sql = ["MATCH (m:公司)-[r:属于]->(n:行业) where m.股票名称 = '{0}' return m.股票代码, m.股票名称, r.级别, n.名称".format(i) for i in entities]
            # print(sql)

        #按行业查询股票
        elif question_type == 'industry_stockget':
            sql = ["MATCH (m:公司)-[r:属于]->(n:行业) where n.名称 = '{0}' return m.股票代码, m.股票名称,n.名称".format(i) for i in entities]
            # print(sql)

        #查询指数类
        elif question_type == 'stockid_indextypeget':
            sql = ["MATCH (m:股票)-[r:成分]->(n:指数) where m.代码 = '{0}' return m.代码, m.名称, n.名称".format(i) for i in entities]

        #查询指数类
        elif question_type == 'stockname_indextypeget':
            sql = ["MATCH (m:股票)-[r:成分]->(n:指数) where m.名称 = '{0}' return m.代码, m.名称, n.名称".format(i) for i in entities]

        #根据指数类查股
        elif question_type == 'indextype_stockget':
            length = len(entities)
            part1_list = []
            part2_list = []
            part3_list = []
            for i in range(length):
                part1_list.append(
                    "(m:股票)-[r#i#:成分]->(n#i#:指数)".replace('#i#', str(i)))
                part2_list.append("n#i#.名称 = '{0}'".format(entities[i]).replace('#i#', str(i)))
                part3_list.append("n#i#.名称".replace('#i#', str(i)))
            part1 = ",".join(part1_list)
            part2 = " and ".join(part2_list)
            part3 = ','.join(part3_list)
            sql = ["MATCH #part1# where #part2# return m.代码, m.名称, #part3#".replace('#part1#', part1).replace('#part2#', part2).replace('#part3#', part3)]

         # 查询公司详情
        elif question_type == 'stockid_cominfoget':
            sql = ["MATCH (m:公司) where m.股票代码 = '{0}' return m.名称,m.股票代码,m.股票名称,m.主营业务, m.公司简介,m.办公地址,m.注册资金".format(i)
                   for i in entities]

        elif question_type == 'stockname_cominfoget':
            sql = [
                "MATCH (m:公司) where m.股票名称 = '{0}' return m.名称,m.股票代码,m.股票名称, m.主营业务, m.公司简介,m.办公地址,m.注册资金".format(i)
                for i in entities]
        elif question_type == 'company_cominfoget':
            sql = [
                "MATCH (m:公司) where m.名称 = '{0}' return m.名称,m.股票代码,m.股票名称,m.主营业务, m.公司简介,m.办公地址,m.注册资金".format(i)
                for i in entities]

        # 查询公司股东
        elif question_type == 'company_stockholderget':
            sql = ["MATCH (m:股东)-[r:投资]->(n:公司) where n.名称 = '{0}' return m.名称, n.名称,n.股票代码".format(i)
                   for i in entities]

        elif question_type == 'stockid_stockholderget':
            sql = [
                "MATCH (m:股东)-[r:投资]->(n:公司) where n.股票代码 = '{0}' return m.名称, n.名称,n.股票代码".format(i)
                for i in entities]

        elif question_type == 'stockname_stockholderget':
            sql = [
                "MATCH (m:股东)-[r:投资]->(n:公司) where n.股票名称 = '{0}' return m.名称, n.名称,n.股票代码".format(i)
                for i in entities]

        #查询子公司
        elif question_type=='company_subcompanyget':
            sql = ["MATCH (m:公司)-[r:控股]->(n:公司) where m.名称 = '{0}' return m.股票代码, m.名称, n.名称".format(i) for i in entities]

        elif question_type=='stockid_subcompanyget':
            sql = ["MATCH (m:公司)-[r:控股]->(n:公司) where m.股票代码 = '{0}' return m.股票代码, m.名称, n.名称".format(i) for i in entities]

        elif question_type=='stockname_subcompanyget':
            sql = ["MATCH (m:公司)-[r:控股]->(n:公司) where m.股票名称 = '{0}' return m.股票代码, m.名称, n.名称".format(i) for i in entities]

        elif question_type == 'stockid_sellsignalget':
            sql = ["MATCH (m:{0})-[r:SellSignalIs]->(n:SellSignal) where m.stock_id = '{1}' return m.stock_id, " \
               "m.stock_name, n.name".format(self.pDate, i) for i in entities]

        elif question_type == 'stockname_sellsignalget':
            sql = ["MATCH (m:{0})-[r:SellSignalIs]->(n:SellSignal) where m.stock_name = '{1}' return m.stock_id, " \
                   "m.stock_name, n.name".format(self.pDate, i) for i in entities]

        elif question_type == 'sellsignal_stockget':
            length = len(entities)
            part1_list = []
            part2_list = []
            part3_list = []
            for i in range(length):
                part1_list.append("(m:{0})-[r#i#:SellSignalIs]->(n#i#:SellSignal)".format(self.pDate).replace('#i#', str(i)))
                part2_list.append("n#i#.name = '{0}'".format(entities[i]).replace('#i#', str(i)))
                part3_list.append("n#i#.name".replace('#i#', str(i)))
            part1 = ",".join(part1_list)
            part2 = " and ".join(part2_list)
            part3 = ','.join(part3_list)
            sql = ["MATCH #part1# where #part2# return m.stock_id, m.stock_name, #part3#".replace('#part1#', part1).replace('#part2#', part2).replace('#part3#', part3)]

        # 查技术形态
        elif question_type == 'stockid_techformget':
            sql = ["MATCH (m:{0})-[r:TechFormIs]->(n:TechForm) where m.stock_id = '{1}' return m.stock_id, " \
                   "m.stock_name, n.name".format(self.pDate, i) for i in entities]

        elif question_type == 'stockname_techformget':
            sql = ["MATCH (m:{0})-[r:TechFormIs]->(n:TechForm) where m.stock_name = '{1}' return m.stock_id, " \
                   "m.stock_name, n.name".format(self.pDate, i) for i in entities]

        elif question_type == 'techform_stockget':
            length = len(entities)
            part1_list = []
            part2_list = []
            part3_list = []
            for i in range(length):
                part1_list.append("(m:{0})-[r#i#:TechFormIs]->(n#i#:TechForm)".format(self.pDate).replace('#i#', str(i)))
                part2_list.append("n#i#.name = '{0}'".format(entities[i]).replace('#i#', str(i)))
                part3_list.append("n#i#.name".replace('#i#', str(i)))
            part1 = ",".join(part1_list)
            part2 = " and ".join(part2_list)
            part3 = ','.join(part3_list)
            sql = ["MATCH #part1# where #part2# return m.stock_id, m.stock_name, #part3#".replace('#part1#', part1).replace('#part2#', part2).replace('#part3#', part3)]

        #查选股动向
        elif question_type == 'stockid_movementget':
            sql = ["MATCH (m:{0})-[r:MovementIs]->(n:Movement) where m.stock_id = '{1}' return m.stock_id, " \
                   "m.stock_name, n.name".format(self.pDate, i) for i in entities]

        elif question_type == 'stockname_movementget':
            sql = ["MATCH (m:{0})-[r:MovementIs]->(n:Movement) where m.stock_name = '{1}' return m.stock_id, " \
                   "m.stock_name, n.name".format(self.pDate, i) for i in entities]

        elif question_type == 'movement_stockget':
            length = len(entities)
            part1_list = []
            part2_list = []
            part3_list = []
            for i in range(length):
                part1_list.append("(m:{0})-[r#i#:MovementIs]->(n#i#:Movement)".format(self.pDate).replace('#i#', str(i)))
                part2_list.append("n#i#.name = '{0}'".format(entities[i]).replace('#i#', str(i)))
                part3_list.append("n#i#.name".replace('#i#', str(i)))
            part1 = ",".join(part1_list)
            part2 = " and ".join(part2_list)
            part3 = ','.join(part3_list)
            sql = ["MATCH #part1# where #part2# return m.stock_id, m.stock_name, #part3#".replace('#part1#', part1).replace('#part2#', part2).replace('#part3#', part3)]

        #按股票代码查询诊股得分
        elif question_type == 'stockid_scoreget':
            sql = ["MATCH (m:{0}) where m.stock_id = '{1}' return m.stock_id, m.stock_name, m.score".format(self.pDate, i) for i in entities]

        elif question_type == 'stockname_scoreget':
            sql = ["MATCH (m:{0}) where m.stock_name = '{1}' return m.stock_id, m.stock_name, m.score".format(self.pDate, i) for i in entities]

        return sql


if __name__ == '__main__':
    handler = QuestionPaser()
